package cz.via.facepalm.calendar;

import cz.via.facepalm.shared.CActivity;

public class ActivityHelper {

	private static final String DELIM = "-";
	private static final String DELIM_TIME = ":";
		
	public static CActivity setWeeklyTime(CActivity act,int day,int hourStart,int minuteStart,int hourEnd,int minuteEnd){
		act.setWeeklyTime(day+DELIM+hourStart+DELIM_TIME+minuteStart+DELIM+hourEnd+DELIM_TIME+minuteEnd);
		return act;
	}
	
	public static int getWeeklyDay(CActivity act){
		return Integer.valueOf(act.getWeeklyTime().split(DELIM)[0]);
	}
	
	public static String getStartTime(CActivity act){
		return fullFormat(act.getWeeklyTime().split(DELIM)[1]);
	}
	
	private static String fullFormat(String string) {
		String[] split = string.split(DELIM_TIME);
		return minCheck(Integer.valueOf(split[0]))+DELIM_TIME+minCheck(Integer.valueOf(split[1]));
	}

	public static String getFinishTime(CActivity act){
		return fullFormat(act.getWeeklyTime().split(DELIM)[2]);
	}
	
	/**
	 * 
	 * @param act
	 * @param percentComming a value from 0 to 1
	 * @return
	 */
	public static String getTimeWithPct(CActivity act,float percentComming){
		String start[] = getStartTime(act).split(DELIM_TIME);
		int startMin = Integer.valueOf(start[0])*60+Integer.valueOf(start[1]);
		int length = getLengthInMinute(act);
		int min = (int) (startMin+(length*percentComming));
		return minCheck(min/60)+DELIM_TIME+minCheck(min%60);
	}

	private static String minCheck(int i) {
		String is = i+"";
		if(i<10){
			is = "0"+is;
		}
		return is;
	}
	
	public static int getLengthInMinute(CActivity act){
		String start[] = getStartTime(act).split(DELIM_TIME);
		String finish[] = getFinishTime(act).split(DELIM_TIME);
		int startMin = Integer.valueOf(start[0])*60+Integer.valueOf(start[1]); 
		int finishMin = Integer.valueOf(finish[0])*60+Integer.valueOf(finish[1]); 
		int diff = finishMin-startMin;
		return diff>0?diff:(diff+(24*60));
	}
	
}
